package edu.zjut.sjq.service;

import edu.zjut.sjq.bean.Article;
import edu.zjut.sjq.bean.Result;
import edu.zjut.sjq.bean.User;
import edu.zjut.sjq.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by sue on 2017/3/26.
 */
@Service
public class ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private YearService yearService;

    // 获取最新上传作品（时间排序前六个）
    public List<Article> getLatestArticle(Integer limitNum,Integer excludeId) {
        try{
            User user= (User) session.getAttribute("user");
            Integer userId=0;
            if (user!=null){
                userId=user.getId();
            }
            return articleMapper.getLatestArticleList(userId,limitNum,excludeId);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    // 获取当届可评分并且当前教师未评分的作品
    public List<Article> getUncheckedArticle(){
        try{
            Integer year=yearService.selectUncheckedYear().getYear();
            Integer userId=((User)session.getAttribute("user")).getId();
            List<Article> articleList = articleMapper.getUncheckedArticle(year, userId);
            for (Iterator<Article> it=articleList.iterator();it.hasNext();){
                Article temp=it.next();
                if (temp.getIsEvaluate()!=0)
                    it.remove();
            }
            return articleList;

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    // 列表按成绩排序
    public List<Article> getArticleListOrderByGreat(){
        Integer year=yearService.selectUncheckedYear().getYear();
        List<Article> articleList = articleMapper.getArticleListOrderByGreat(year);
        updateArticleGreat(articleList);
        return articleList;
    }

    // 点赞数量排序
    public List<Article> getArticleListOrderByLike(){
        Integer year=yearService.selectUncheckedYear().getYear();
        List<Article> articleList = articleMapper.getArticleListOrderByLike(year);
        return articleList;
    }


    //
    @Async
    public void updateArticleGreat(List<Article> articleList){
        for (Article temp : articleList){
            articleMapper.updateArticleGrate(temp.getId(),temp.getAvgGreat());
        }
    }




    //  上传路径等描述到数据库
    public Result saveArticle(Article article){
        Result result = new Result();
        result.setRes(0);
        User user = (User) session.getAttribute("user");
        if (user==null){
            result.setMsg("您好，请先登录");
            return  result;
        }

        article.setCreateTime(new Date());
        article.setTag(0);
        article.setGreat(0.0);
        article.setUserId(user.getId());
        Integer res=articleMapper.insert(article);
        if (res==1){
            result.setRes(1);
            result.setMsg("上传成功！");
        }else
            result.setMsg("保存失败！");

        return result;
    }

    /**
     * 根据条件综合获取article列表，列表按提交时间排序
     * @param begin 查询起始时间
     * @param year 第几届
     * @param type 作品类型
     * @param tag 知否为可评分作品
     * @return
     */
    public List<Article> getArticleListOrderByTime(Integer year,Integer type,Integer tag,Date begin){
        User user= (User) session.getAttribute("user");
        Integer userId=0;
        if (user!=null)
            userId=user.getId();
        return articleMapper.getArticleListOrderByTime(year,type,tag,begin,userId);
    }

    // 根据article id从数据库获取路径等描述
    public Article getArticleById(Integer articleId){
        User user= (User) session.getAttribute("user");
        Integer userId=0;
        if (user!=null)
            userId=user.getId();
        return articleMapper.getArticleById(articleId,userId);
    }
}
